package com.lianniu.mgr.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.cms.dao.IMainNavigationDao;
import com.lianniu.mgr.cms.model.MainNavigationVo;
import com.lianniu.mgr.cms.service.IMainNavigationService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;

@Service
public class MainNavigationServiceImpl implements IMainNavigationService {

	private final static Logger logger = Logger.getLogger(MainNavigationServiceImpl.class);
	  
	@Resource
	IMainNavigationDao mainNavigationDao;
	
	@Override
	public BaseResult insertMainNavigation(MainNavigationVo curMainNavigationVo) {
		// TODO Auto-generated method stub
	    logger.info("insertMainNavigation begin");
	    logger.info("insertMainNavigation: input data check begin");
	    logger.info("insertMainNavigation curNavigationVo:" +JSON.toJSONString(curMainNavigationVo));	   
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curMainNavigationVo.getMsSortLink())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }	    
	    if (StringUtil.isEmpty(curMainNavigationVo.getMsSortName())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }  
	    if (StringUtil.isEmpty(curMainNavigationVo.getSort())) {
	    	curMainNavigationVo.setSort("0");
		 }  
		    
	   
	    logger.info("insertMainNavigation: input data check end");	    
	    return BaseResult.success(mainNavigationDao.insertMainNavigation(curMainNavigationVo));
	}

	@Override
	public BaseResult deleteById(String Id) {
	   // TODO Auto-generated method stub
	   logger.info("deleteById begin");
	   logger.info("deleteById Id:" +Id);
					  
	   return BaseResult.success(mainNavigationDao.deleteById(Id));
	}

	@Override
	public BaseResult deleteMainNavigationBatch(String... id) {
		// TODO Auto-generated method stub
	    logger.info("deleteMainNavigationBatch begin");
	    logger.info("deleteMainNavigationBatch Ids:" +id.toString());
	    logger.info("deleteMainNavigationBatch: input data check begin");
	   
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(id.toString())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    logger.info("deleteMainNavigationBatch: input data check end");
	    
	    return BaseResult.success(mainNavigationDao.deleteMainNavigationBatch(id));
	}

	@Override
	public BaseResult updateById(MainNavigationVo curMainNavigationVo) {
		// TODO Auto-generated method stub
	    logger.info("updateById begin");
	    logger.info("updateById: update data check begin");
	    logger.info("updateById curMainNavigationVo:" +JSON.toJSONString(curMainNavigationVo));
	  
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curMainNavigationVo.getMsSortLink())) {
		      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		    }	    
		    if (StringUtil.isEmpty(curMainNavigationVo.getMsSortName())) {
		      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		    }  
		    if (StringUtil.isEmpty(curMainNavigationVo.getSort())) {
		    	curMainNavigationVo.setSort("0");
			 }		    
	    
	    logger.info("updateById: update data check end");
	    
	    return BaseResult.success(mainNavigationDao.updateById(curMainNavigationVo));
	}

	@Override
	public BaseResult selectByMainNavigationForPage(MainNavigationVo curMainNavigationVo,PaginationInfo paginationInfo) {
		// TODO Auto-generated method stub
	    logger.info("selectByMainNavigationForPage begin.");
	    if (!StringUtil.isEmpty(curMainNavigationVo.getMsSortName())) {
	    	curMainNavigationVo.setMsSortName(curMainNavigationVo.getMsSortName().replace(" ", ""));
	    }
	    logger.info("selectByMainNavigationForPage TsnName:" +curMainNavigationVo.getMsSortName());
	
	    logger.info("selectByMainNavigationForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
	    
	    curMainNavigationVo.setPaginationInfo(paginationInfo);
	    List<MainNavigationVo> list=mainNavigationDao.selectByMainNavigationForPage(curMainNavigationVo);
	    Map<String,Object> retmap=new HashMap<String,Object>();
	    retmap.put("total", paginationInfo.getTotalRecord());
	    retmap.put("curPage", paginationInfo.getCurPage());
	    retmap.put("rows", list);
	    logger.info("selectByMainNavigationForPage:  end");    
	    
	    return BaseResult.success(retmap); 
	}

	@Override
	public BaseResult selectById(MainNavigationVo curMainNavigationVo) {
		// TODO Auto-generated method stub
	    logger.info("selectById begin");
	    logger.info("selectById Id:" +curMainNavigationVo.getId());
	  
	    return BaseResult.success(mainNavigationDao.selectById(curMainNavigationVo.getId()));
	}

	@Override
	public Boolean checkMsSortName(String msSortName) {
		// TODO Auto-generated method stub
	    logger.info("checkTsnName begin");
	    logger.info("checkTsnName msSortName:" +msSortName);
	    logger.info("checkTsnName: input data check begin");   
	  
	    if (StringUtil.isEmpty(msSortName)) {       
	      return false;
	    }
	    logger.info("checkTsnName: input data check end");
	    
	    MainNavigationVo mainNavigationVo=new MainNavigationVo();	   
	    mainNavigationVo.setMsSortName(msSortName);
	    int count=mainNavigationDao.count(mainNavigationVo);
	    if(count>0){
	      return false;      
	    }
	    else{
	      return true;
	    }   
	}

}
